﻿using IBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase,IUserBll
    {
        private readonly IUserBll bll;
        private readonly ILogger<UserController> log;

        public UserController(IUserBll bll,ILogger<UserController> log)
        {
            this.bll = bll;
            this.log = log;
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="userInfo">用户表</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost("Add")]
        public int Add(UserInfo userInfo)
        {
            try
            {
                log.LogInformation($"用户{userInfo.UserName}在{DateTime.Now}进行了注册");
                return bll.Add(userInfo);

            }
            catch (Exception ex)
            {
                log.LogError($"报异常了，异常信息是{ex.Message}");
                throw;
            }
           
        }
        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet("Number")]
        public string Number()
        {
            try
            {
                log.LogInformation($"用户在获取验证码");
                Random random = new Random();
                return random.Next(1000, 10000).ToString();

            }
            catch (Exception ex)
            {
                log.LogError($"报异常了，异常信息是{ex.Message}");
                throw;
            }
           
        }
        /// <summary>
        /// 验证验证码是否匹配
        /// </summary>
        /// <param name="code">自己输入的验证码</param>
        /// <param name="codes">自动生成的验证</param>
        /// <returns></returns>
        [HttpGet("Code")]
        public int Code(string code = "", string codes = "")
        {
            if (code != codes)
            {
                return -1;
            }
            return 0;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns>通过用户名和密码查询返回相应的值</returns>
        [HttpPost("Login")]
        public dynamic Login(string name, string pwd)
        {
            try
            {
                log.LogInformation($"用户{name}在{DateTime.Now}进行了登录");
                return bll.Login(name, pwd);

            }
            catch (Exception ex)
            {
                log.LogError($"报异常了异常信息是{ex.Message}");
                throw;
            }
            
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="name">姓名</param>
        /// <returns>通过登录人获取身份在获取信息</returns>
        [HttpGet("Show")]
        public IQueryable<UserInfo> Show(string name = "")
        {
            try
            {
                log.LogInformation($"用户:{name}查看显示");
                return bll.Show(name);
            }
            catch (Exception ex)
            {
                log.LogError($"报异常了:{ex.Message}");
                throw;
            }
           
        }
    }
}
